package com.hnkywl.sts.service.jhgl;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hnkywl.sts.dao.jhgl.BgjhhzmxDao;
import com.hnkywl.sts.dao.jhgl.BgjhmxDao;
import com.hnkywl.sts.entity.jhgl.Bgjhhzmx;
import com.hnkywl.sts.entity.jhgl.Bgjhmx;
import com.hnkywl.sts.entity.sys.Yg;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.util.ReflectionUtils;
import com.hnkywl.sts.webservice.bg.request.PlanBack;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: ZHUCHG
 * Date: 13-7-3
 * Time: 上午11:14
 * To change this template use File | Settings | File Templates.
 */
@Service
public class BgjhhzmxService {
    private BgjhhzmxDao bgjhhzmxDao;
    private BgjhmxDao bgjhmxDao;


    public Bgjhhzmx find(Long id) {
        return bgjhhzmxDao.get(id);
    }

    //供应站处理
    @Transactional
    public void gyzcl(List<Bgjhhzmx> bgjhhzmxList) {
        try {
            changeGyzZt(bgjhhzmxList, 3);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //供应站退回
    @Transactional
    public void gyzth(List<Bgjhhzmx> bgjhhzmxList) {
        try {
            changeGyzZt(bgjhhzmxList, 10);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void changeGyzZt(List<Bgjhhzmx> bgjhhzmxList, Integer zt) {
        for (int i = 0; i < bgjhhzmxList.size(); i++) {
            bgjhhzmxList.get(i).setZt(zt);
            bgjhhzmxList.get(i).setGyzclr(YgService.getCurrentYg());
            Yg yg = YgService.getCurrentYg();
            bgjhhzmxList.get(i).setGyzclbm(yg.getZgjg());
            bgjhhzmxList.get(i).setGyzclrq(new Date());
            if (bgjhhzmxList.get(i).getDhrq() == null) {
                Calendar cal = Calendar.getInstance();
                cal.add(Calendar.MONTH, 1);
                bgjhhzmxList.get(i).setDhrq(cal.getTime());
            }
            if (zt == 10) {
                bgjhhzmxList.get(i).setThr(YgService.getCurrentYgName());
                bgjhhzmxList.get(i).setThrq(new Date());
                Long ggId = bgjhhzmxList.get(i).getWzgg().getId();
                Long hzmxId = bgjhhzmxList.get(i).getId();
                List<Object> ids = bgjhmxDao.findJhmxByHzmx(ggId, hzmxId);
                for (Object id : ids) {
                    BigDecimal tempId = (BigDecimal) id;
                    Bgjhmx bgjhmx = bgjhmxDao.get(tempId.longValue());
                    if (bgjhmx != null)
                        bgjhmx.setZt(2);
                }
            } else {
                bgjhhzmxList.get(i).setSbsl(bgjhhzmxList.get(i).getJhsl() - bgjhhzmxList.get(i).getSl1());
            }
            Bgjhhzmx temp = bgjhhzmxDao.get(bgjhhzmxList.get(i).getId());
            temp.setLastUpdated(new Date());
            ReflectionUtils.getInstance(temp, bgjhhzmxList.get(i));
        }
    }

    //业务科处理
    @Transactional
    public void ywkcl(List<Bgjhhzmx> bgjhhzmxList) {
        changeYwkZt(bgjhhzmxList, 4);
    }

    //业务科退回
    @Transactional
    public void ywkth(List<Bgjhhzmx> bgjhhzmxList) {
        changeYwkZt(bgjhhzmxList, 10);
    }

    /**
     * 接口保供计划退回
     *
     * @param list
     * @return
     */
    @Transactional
    public String InterfacePlanBack(List<PlanBack> list) {
        try {
            for (PlanBack planBack : list) {
                if (null != planBack.getStopflag() && planBack.getStopflag().equals("1")) {
                    Bgjhhzmx bgjhhzmx = this.find(planBack.getDetailId());
                    bgjhhzmx.setZt(10);
                    bgjhhzmx.setThr(planBack.getOperator());
                    bgjhhzmx.setThrq(new Date());
                    bgjhhzmx.setThyy(planBack.getReason());
                    Long ggId = bgjhhzmx.getWzgg().getId();
                    Long hzmxId = bgjhhzmx.getId();
                    List<Object> ids = bgjhmxDao.findJhmxByHzmx(ggId, hzmxId);
                    for (Object id : ids) {
                        BigDecimal tempId = (BigDecimal) id;
                        Bgjhmx bgjhmx = bgjhmxDao.get(tempId.longValue());
                        bgjhmx.setZt(2);
                    }
                    Bgjhhzmx temp = bgjhhzmxDao.get(bgjhhzmx.getId());
                    temp.setLastUpdated(new Date());
                    ReflectionUtils.getInstance(temp, bgjhhzmx);
                } else {
                    Bgjhhzmx bgjhhzmx = this.find(planBack.getDetailId());
                    bgjhhzmx.setZt(4);
                    bgjhhzmx.setThr(planBack.getOperator());
                    bgjhhzmx.setThrq(new Date());
                    bgjhhzmx.setThyy(planBack.getReason());
                    Long ggId = bgjhhzmx.getWzgg().getId();
                    Long hzmxId = bgjhhzmx.getId();
                    List<Object> ids = bgjhmxDao.findJhmxByHzmx(ggId, hzmxId);
                    for (Object id : ids) {
                        BigDecimal tempId = (BigDecimal) id;
                        Bgjhmx bgjhmx = bgjhmxDao.get(tempId.longValue());
                        bgjhmx.setZt(1);
                    }
                    Bgjhhzmx temp = bgjhhzmxDao.get(bgjhhzmx.getId());
                    temp.setLastUpdated(new Date());
                    ReflectionUtils.getInstance(temp, bgjhhzmx);
                }
            }
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private void changeYwkZt(List<Bgjhhzmx> bgjhhzmxList, Integer zt) {
        for (int i = 0; i < bgjhhzmxList.size(); i++) {
            bgjhhzmxList.get(i).setZt(zt);
            bgjhhzmxList.get(i).setYwkclr(YgService.getCurrentYg());
            Yg yg = YgService.getCurrentYg();
            bgjhhzmxList.get(i).setYwkclbm(yg.getZgjg());
            bgjhhzmxList.get(i).setYwkclrq(new Date());
            if (zt == 10) {
                bgjhhzmxList.get(i).setThr(YgService.getCurrentYgName());
                bgjhhzmxList.get(i).setThrq(new Date());
                Long ggId = bgjhhzmxList.get(i).getWzgg().getId();
                Long hzmxId = bgjhhzmxList.get(i).getId();
                List<Object> ids = bgjhmxDao.findJhmxByHzmx(ggId, hzmxId);
                for (Object id : ids) {
                    BigDecimal tempId = (BigDecimal) id;
                    Bgjhmx bgjhmx = bgjhmxDao.get(tempId.longValue());
                    bgjhmx.setZt(2);
                }
            } else {
                //该物资是否有指标合同
                boolean y = bgjhhzmxDao.sfybzbght(bgjhhzmxList.get(i).getId());
                //cgfs=1比价  cgfs=2招标
                bgjhhzmxList.get(i).setCgfs(y ? 2 : 1);
                if (y) {
                    bgjhhzmxList.get(i).setZt(7);
                }
            }
            Bgjhhzmx temp = bgjhhzmxDao.get(bgjhhzmxList.get(i).getId());
            temp.setLastUpdated(new Date());
            ReflectionUtils.getInstance(temp, bgjhhzmxList.get(i));
        }
    }

    //保存到货日期
    @Transactional
    public void saveJhyq(Bgjhhzmx bgjhhzmx) {
        Bgjhhzmx clone = this.find(bgjhhzmx.getId());
        //clone = (Bgjhhzmx) ReflectionUtils.getInstance(clone, bgjhhzmx);
        clone.setDhrq(bgjhhzmx.getDhrq());
        clone.setLastUpdated(new Date());
        this.bgjhhzmxDao.save(clone);
    }

    public String findYwkList(Integer status, Long jhdwId, String wzdl, Integer jhnf, Integer jhyf, Date dhrq1, Date dhrq2, String wzbm, String sm, String ywksbcl, int pageNo, int pageSize) {
        Page<Map> page = bgjhhzmxDao.findYwkList(status, jhdwId, wzdl, jhnf, jhyf, dhrq1, dhrq2, wzbm, sm, ywksbcl, pageNo, pageSize);
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        String json = gson.toJson(page.getResult());
        return "{\"total\":" + page.getTotalCount() + ",\"rows\":" + json + "}";
    }

    public String findKcByBjjhhzmx(Long id) {
        List<Map> list = bgjhhzmxDao.findKcByBjjhhzmx(id);
        Gson gson = new Gson();
        String json = gson.toJson(list);
        return "{\"rows\":" + json + "}";
    }

    /**
     * 查询向保供发送失败的记录，重新发送
     *
     * @return
     */
    public List<Bgjhhzmx> findFailList() {
        String hql = "from Bgjhhzmx where sfcg = 0 and deleted=0 and zt=0  ";
        return this.bgjhhzmxDao.find(hql);
    }

    @Autowired
    public void setBgjhhzmxDao(BgjhhzmxDao bgjhhzmxDao) {
        this.bgjhhzmxDao = bgjhhzmxDao;
    }

    @Autowired
    public void setBgjhmxDao(BgjhmxDao bgjhmxDao) {
        this.bgjhmxDao = bgjhmxDao;
    }

}
